package com.iflytek.uoamp.notify.msg.plugins;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.iflytek.uoamp.common.notify.constant.ConstField;
import com.iflytek.uoamp.common.notify.enumerate.ContactState;
import com.iflytek.uoamp.common.notify.enumerate.ContentType;
import com.iflytek.uoamp.common.notify.exception.NotifyException;
import com.iflytek.uoamp.common.notify.format.Formatter;
import com.iflytek.uoamp.common.notify.obj.Contact;
import com.iflytek.uoamp.common.notify.plugins.AbstractPlugin;
import com.iflytek.uoamp.common.notify.plugins.PluginAction;
import com.iflytek.uoamp.common.notify.util.Configurator;
import com.iflytek.uoamp.notify.msg.format.FormatFocusMsg;
import com.iflytek.uoamp.notify.msg.format.FormatMsgFactory;
import com.iflytek.uoamp.notify.msg.format.MsgFormatter;

public class MessagePlugin extends AbstractPlugin{
	
    private static final String MSG_FILE = "text.msg";
    private static final String MSG_ACTION = "plugins.msg.action";

	@Override
	public Formatter getFormatter() {
	    if(formatter != null) {
	        return formatter;
	    }
	    
		if(useTemplate) {
            if (notification.getExtend().containsKey(ConstField.FOCUS_WARN_FLAG)) {
                formatter = new FormatFocusMsg();
            } else {
                ContentType type = ContentType.getType(notification.getContentType());
                formatter = FormatMsgFactory.getMsgFormatter(type);
            }

		} else {
			formatter = new MsgFormatter();
		}
		
		return formatter;
	}

	@Override
	public boolean sendContent(String content, List<Contact> contacts) {
	    boolean ignored = true;
	    boolean ret = true;
	    
	    for (Contact contact : contacts) {
            if (StringUtils.isBlank(contact.getPhone())) {
                contact.setState(ContactState.IGNORED);
                logInfo.addInfo(MessageFormat.format(ConstField.PASSBYCONTACT, contact.getName(), ConstField.BLANKPHONENUMBER));
                continue;
            }
            ignored = false;
            try {
                List<String> phone = new ArrayList<String>();
                phone.add(contact.getPhone());
                
                String path = writeToFile(MSG_FILE, getJSON(phone, content));
                String[] params = {"-f", path};
                String pluginPath = Configurator.getInstance().getPropertyByKey(MSG_ACTION, "");
                PluginAction.run(pluginPath, params);
                setState(contacts, ContactState.SUCCESS);
                logInfo.addInfo(MessageFormat.format(ConstField.SENDINGMSG, 
                        formatter.getContent(content), contact.getPhone()));
                ret &= true;
            } catch (NotifyException e) {
                setState(contacts, ContactState.FAILED);
                logInfo.addError(e.getMessage());
                ret &= false;
            }
        }

        if (ignored) {
            logInfo.addError(MessageFormat.format(ConstField.DISABLEPLUGIN, "msg", ConstField.NOPROPERCONTACTS));
            return false;
        }
        
        return ret;
	}
}
